﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain
{
	using System;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	public class UserBadge : EntityBase<UserBadge>
	{
		// private fields
		private User _user;

		/// <summary>
		/// Initializes a new instance of the <see cref="UserBadge"/> class.
		/// </summary>
		public UserBadge()
		{
			DeterminePermission(Roles.UsersBadges);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UserBadge"/> class.
		/// </summary>
		/// <param name="userId">The user id.</param>
		/// <param name="title">The title.</param>
		/// <param name="reputation">The reputation.</param>
		public UserBadge(int userId, string title, int reputation) 
			: this()
		{
			this.UserId = userId;
			this.Title = title;
			this.Reputation = reputation;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UserBadge"/> class.
		/// </summary>
		/// <param name="userId">The user id.</param>
		/// <param name="title">The title.</param>
		/// <param name="fileName">Name of the file.</param>
		/// <param name="filePath">The file path.</param>
		/// <param name="fileType">Type of the file.</param>
		/// <param name="reputation">The reputation.</param>
		public UserBadge(int userId, string title, string fileName, string filePath, string fileType, int reputation) 
			: this(userId, title, reputation)
		{
			this.FileName = fileName;
			this.FilePath = filePath;
			this.FileType = fileType;
		}

		/// <summary>
		/// Gets or sets the user id.
		/// </summary>
		/// <value>The user id.</value>
		public virtual int UserId
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public virtual string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the slug.
		/// </summary>
		/// <value>The slug.</value>
		public virtual string Slug
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the body.
		/// </summary>
		/// <value>The body.</value>
		public virtual string Body
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the body parsed.
		/// </summary>
		/// <value>The body parsed.</value>
		public virtual string BodyParsed
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the name of the file.
		/// </summary>
		/// <value>The name of the file.</value>
		public virtual string FileName
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the name of the file on disk.
		/// </summary>
		/// <value>The name of the file disk.</value>
		public virtual string FileDiskName
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the file path.
		/// </summary>
		/// <value>The file path.</value>
		public virtual string FilePath
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the full path.
		/// </summary>
		/// <value>The full path.</value>
		public virtual string FullPath
		{
			get
			{
				return string.Concat(this.FilePath, "/", this.FileDiskName);
			}
		}

		/// <summary>
		/// Gets or sets the type of the file.
		/// </summary>
		/// <value>The type of the file.</value>
		public virtual string FileType
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the reputation.
		/// </summary>
		/// <value>The reputation.</value>
		public virtual int Reputation
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the count.
		/// </summary>
		/// <value>The count.</value>
		public virtual int Count
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the scope.
		/// </summary>
		/// <value>The scope.</value>
		public virtual UserBadgeScope Scope
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the date created.
		/// </summary>
		/// <value>The created.</value>
		public virtual DateTime Created
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the user.
		/// </summary>
		/// <value>The user.</value>
		public virtual User User
		{
			get
			{
				return _user;
			}
			set
			{
				_user = value;

				if (_user != null)
				{
					this.UserId = _user.Id;
				}
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<UserBadge> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Title)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));

			rules.Add(x => x.Body)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Description));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}